/*
 * @Author: your name
 * @Date: 2021-08-25 11:37:11
 * @LastEditTime: 2021-08-25 20:23:44
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /backend/src/main/java/com/buaa/demo/Controller/UserController.java
 */
package com.buaa.demo.Controller;

import com.buaa.demo.Entity.User;
import com.buaa.demo.Service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class UserController {

    @Autowired
    public UserService userService;

    @PostMapping("/changePassword")
    public Map<String, Object> changePssword(@RequestBody Map<String, String> remap){
        StaticPrint.printArgs("change password", remap);
        String email = (String)remap.get("email");
        String password = remap.get("password");
        Map<String, Object> resMap = new HashMap<>();
        try{
            userService.changePassword(email, password);
        } catch (Exception e){
            e.printStackTrace();
            resMap.put("res", false);
            resMap.put("message", "数据库异常，请联系管理员解决");
            return resMap;
        }
        resMap.put("res", true);
        resMap.put("message", "修改密码成功");
        return resMap;
    }

    @PostMapping("/ifUserExists")
    public Map<String, Object> checkExistence(@RequestBody Map<String, String> remap){
        StaticPrint.printArgs("check exitence request", remap);
        String email = (String)remap.get("email");
        Map<String, Object> resMap = new HashMap<>();
        try{
            User u = userService.getUserByEmail(remap.get("email"));
            if(u == null){
                resMap.put("res", false);
                resMap.put("message", "该邮箱尚未注册，请前往注册");
            } else{
                resMap.put("res", true);
                resMap.put("message", "检查用户存在");
            }
            return resMap;
        } catch (Exception e){
            e.printStackTrace();
            resMap.put("res", false);
            resMap.put("message", "数据库异常，请联系管理员解决");
            return resMap;
        }
    }
}
